﻿function createRequest() {
	var result = null;
	if (window.XMLHttpRequest) {
	// FireFox, Safari, etc.
		result = new XMLHttpRequest();
		if (typeof result.overrideMimeType != 'undefined') {
			result.overrideMimeType('application/xml'); 
		}
	}
	else if (window.ActiveXObject) {
	// MSIE
		result = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	else {
	// aborting the application
	}
	return result;
}


//runs onload
//gets list of available categories and then prints it out to an unordered list
function init(){
	
	var url ="http://localhost:8080/WebPage/resources/article/categories"; 
	var req = createRequest(); // defined above
	// Create the callback:
	req.onreadystatechange = function() {
		if (req.readyState != 4) return; 
			if (req.status != 200) {
		// Handle request fail here...
		return;
		}
		// Request successful, read the response
		var xmlDoc = req.responseXML;
		var path = "/list/name";
		var nodes = xmlDoc.selectNodes(path);
		
		var catStart = document.getElementById("cat");
		for( i = 0; i < nodes.length; i++){

			var catElement = document.createElement("li");
			var catLink = document.createElement("a");
			catLink.setAttribute("href", "#");
			catLink.setAttribute("onClick", "browseCategory(this.innerHTML)");
			catStart.appendChild(catElement);
			var catText = document.createTextNode(nodes[i].childNodes[0].nodeValue);
			catLink.appendChild(catText);
			catElement.appendChild(catLink);

		}
	};

	req.open("GET", url, true);
	req.send();
	
}

//browse for articles from a specific category
//runs when a category is clicked
//takes one argument - cat, the name of the category
function browseCategory(cat){

	var url ="http://localhost:8080/WebPage/resources/article/categories/"+cat; 
	
	var req = createRequest(); // defined above
	req.onreadystatechange = function() {
		if (req.readyState != 4) return; 
			if (req.status != 200) {
		// Handle request fail here..
				return;
		}
		// Request successful, read the response
		var articleIdXML = req.responseXML;
		
		var path = "/list/article";
		var nodes = articleIdXML.selectNodes(path);
		
		var articleStart = document.getElementById("maincontent");
		var listStart = document.createElement("ul");
		var h1 = document.createElement("h1");
		var h1Text = document.createTextNode(cat);
		var resultText = document.createTextNode("Found " + nodes.length + " article(s)");
		
		articleStart.innerHTML = "";
		listStart.appendChild(resultText);
		h1.appendChild(h1Text);
		articleStart.appendChild(h1);
		articleStart.appendChild(listStart);
		
		for( i = 0; i < nodes.length; i++){
			
			var listL = document.createElement("li");
			var breakLine = document.createElement("br");
			var articleHeadline = document.createElement("a");
			var articleHeadlineText = document.createTextNode(nodes[i].childNodes[1].childNodes[0].nodeValue);
			articleHeadline.setAttribute("href", "#");
			articleHeadline.setAttribute("id", nodes[i].childNodes[0].childNodes[0].nodeValue);
			articleHeadline.setAttribute("onClick", "browseArticles(this.id)"); 
			articleHeadline.appendChild(articleHeadlineText);
			listL.appendChild(articleHeadline);
			listStart.appendChild(listL);
			
		}			
	};

	req.open("GET", url, true);
	req.send();

}

//browse a specific article
//runs when an article is clicked
//takes one argument - articleID, the id for the specific article
function browseArticles(articleID){
	
	var url ="http://localhost:8080/WebPage/resources/article/articleid/"+articleID; 
	
	var req = createRequest(); // defined above
	// Create callback:
	req.onreadystatechange = function() {
		if (req.readyState != 4) return; // Not there yet
			if (req.status != 200) {
		// Handle request failure
				return;
		}
		// Request successful, read the response
		var articleXML = req.responseXML;	
		var path = "/article";
		var nodes = articleXML.selectNodes(path);
		var mainContent = document.getElementById("maincontent");
		
		mainContent.innerHTML = "";
		
		var articleAuthor = document.createElement("p");
		var articleDate = document.createElement("p");
		var articleContent = document.createElement("p");
		var articleHeadline = document.createElement("h1");
		
		var authorText = document.createTextNode("Author: " + nodes[0].childNodes[2].childNodes[0].nodeValue);
		var contentText = document.createTextNode(nodes[0].childNodes[0].childNodes[0].nodeValue);
		var dateText = document.createTextNode("Published: " + nodes[0].childNodes[3].childNodes[0].nodeValue);
		var headlineText = document.createTextNode(nodes[0].childNodes[4].childNodes[0].nodeValue);
			
		articleAuthor.appendChild(authorText);
		articleContent.appendChild(contentText);
		articleDate.appendChild(dateText);
		articleHeadline.appendChild(headlineText);
		
		mainContent.appendChild(articleHeadline);
		mainContent.appendChild(articleAuthor);
		mainContent.appendChild(articleDate);
		mainContent.appendChild(articleContent);		
		
	};

	req.open("GET", url, false);
	req.send();
}